package com.lpc.android.abc.main.mvp;

import android.util.Log;

import com.lpc.android.abc.base.api.ApiCallback;
import com.lpc.android.abc.base.base.BaseActivity;
import com.lpc.android.abc.base.base.BaseFragment;
import com.lpc.android.abc.base.base.BasePresenter;
import com.lpc.android.abc.base.bean.HomeCampaign;
import com.lpc.android.abc.base.bean.Ware;
import com.lpc.android.abc.base.utils.LogUtil;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by lipc0113 on 2017/6/16 0016.
 */

@SuppressWarnings("unchecked")
public class MainPresenter extends BasePresenter {

    private MainContract.HomeInfoView homeInfoView;
    private final MainModel mModel;

    public void setHomeInfoView(MainContract.HomeInfoView homeInfoView) {
        this.homeInfoView = homeInfoView;
    }

    private MainContract.WareListView wareListView;

    public void setWareListView(MainContract.WareListView wareListView) {
        this.wareListView = wareListView;
    }

    public MainPresenter(MainModel mModel) {
        this.mModel = mModel;
    }

    /*private static class MainPresenterHolder {

        private static final MainPresenter mainPresenter = new MainPresenter();
    }

    public static final MainPresenter getInstance() {
        return MainPresenterHolder.mainPresenter;
    }*/

    public void getHomeInfo() {

        LogUtil.e(this.toString());
        mModel.getHomeInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseFragment) homeInfoView).<List<HomeCampaign>>bindToLifecycle())
                .subscribe(newMySubscriber(new ApiCallback<List<HomeCampaign>>() {
                    @Override
                    public void onNext(List<HomeCampaign> homeCampaigns) {

                        if (homeInfoView != null) {
                            homeInfoView.homeInfoSuccess(homeCampaigns);
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {

                        LogUtil.e(throwable.getMessage());
                        if (homeInfoView != null) {
                            homeInfoView.homeInfoFail(throwable.getMessage());
                        }

                    }
                }));

    }

    public void getWaresList(Map<String, String> map, final boolean isRefresh) {

        LogUtil.e(this.toString());
        mModel.getWaresList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(((BaseActivity) wareListView).<Ware>bindToLifecycle())
                .subscribe(newMySubscriber(new ApiCallback<Ware>() {
                    @Override
                    public void onNext(Ware ware) {

                        LogUtil.e(ware.toString());

                        if (wareListView != null) {
                            wareListView.waresListSuccess(ware, isRefresh);
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {

                        LogUtil.e(throwable.getMessage());
                        if (wareListView != null) {
                            wareListView.waresListFail(throwable.getMessage(), isRefresh);
                        }
                    }
                }));
    }
}
